import requests
import random
import configparser

cf = configparser.ConfigParser()

cf.read("config.ini",encoding='utf-8')
port = cf.get('clash api', 'port')
secret = cf.get('clash api', 'secret')
selector = cf.get('clash api', 'selector')

port = port
secret = secret

headers_secret = {
        'Authorization': 'Bearer {}'.format(secret)
    }

url_all_proxies_info = 'http://localhost:{}/proxies'.format(port)
url_all_proxies_info = requests.get(url_all_proxies_info,headers=headers_secret).json()
url_all_proxies = 'http://localhost:{}/proxies/{}'.format(port,selector)

def change_proxy(url_all_proxies):
    res_proxies = requests.get(url_all_proxies,headers=headers_secret).json()
    now_proxy = res_proxies['now']
    print('现在的代理是{}'.format(now_proxy))
    proxy_name_list = res_proxies['all'][3:-3]
    if now_proxy not in ['DIRECT','REJECT','账号邮箱看最新的地址','NETV2','自动选择','故障转移']:
        proxy_name_list.remove(now_proxy)

    while True:
        random_proxy = random.choice(proxy_name_list)
        delay = url_all_proxies_info['proxies'][random_proxy]['history'][-1]['delay']
        if delay!=0:
            break
    print('随机选择的代理为{}'.format(random_proxy))
    data = {
        "name": random_proxy
    }
    headers = {
        "content-type": "application/json",
        'Authorization': 'Bearer {}'.format(secret)
    }

    res = requests.put(url=url_all_proxies,json=data,headers=headers)
    print('切换代理请求的状态码为{}'.format(res.status_code))
    if res.status_code == 204:
        print('切换代理成功！现在的代理为{}'.format(random_proxy))


# 1、脚本介绍
# Python语言实现Clash
# for windows自动切换国家节点
# 2、脚本目的
# 当使用Python语言爬取网站并开启clash
# for windows时，ip即节点被ban后，实现Clash for windows自动切换国家节点
# 3、官方文档
# https://clash.gitbook.io/doc/restful-api
# 4、配置
# (1)
# 查看port和secret
# 进入Clash
# for windows常规页面 -->打开目录

if __name__ == '__main__':
    change_proxy(url_all_proxies=url_all_proxies)